package com.vogue.family.data.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.vogue.component.retrofit.VogueRetrofit;
import com.vogue.family.data.interceptor.TokenInterceptor;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetWorkManager {

    private MeetingApiService apiService;

    private static class NetWorkManagerHolder {
        private static final NetWorkManager INSTANCE = new NetWorkManager();
    }

    public static final NetWorkManager getInstance() {
        return NetWorkManagerHolder.INSTANCE;
    }

    private NetWorkManager(){

        //Gson日期时间转换
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create();

        OkHttpClient mOkHttpClient = VogueRetrofit.getInstance()
                .createClientBuilder()
                .addInterceptor(new TokenInterceptor())
                .build();


        Retrofit mRetrofit = new Retrofit.Builder()
                .baseUrl(MeetingApiService.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//使用rxjava2 库：adapter-rxjava2
                .addConverterFactory(GsonConverterFactory.create(gson))//使用gson 库：converter-gson
                .client(mOkHttpClient)
                .build();

        apiService = mRetrofit.create(MeetingApiService.class);

    }

    public MeetingApiService getApiService() {
        return apiService;
    }

}
